package com.ponhu.ea2.goodsCenter.provider.facade.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.ponhu.ea2.entity.ApiGoodsForOrder;
import com.ponhu.ea2.entity.CloudTradeGoods;
import com.ponhu.ea2.enums.SalesModelEnum;
import com.ponhu.ea2.enums.YesOrNoEnum;
import com.ponhu.ea2.goodsCenter.provider.config.DBSourceConfig;
import com.ponhu.ea2.goodsCenter.provider.mapper.ApiGoodsForOrderMapper;
import com.ponhu.ea2.goodsCenter.provider.service.CloudTradeGoodsService;
import com.ponhu.ea2.service.facade.GoodsForOrderApiService;
import com.ponhu.ea2.vo.ApiGoodsForOrderVo;
import com.ponhu.ea2.vo.ApiGoodsForScmVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单查询商品信息实现类
 *
 * @author zhaokai
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = GoodsForOrderApiService.class, timeout = 6000)
public class GoodsForOrderApiServiceImpl implements GoodsForOrderApiService {

    @Resource
    ApiGoodsForOrderMapper apiGoodsForOrderMapper;
    @Resource
    CloudTradeGoodsService cloudTradeGoodsService;

    @Override
    @DS(DBSourceConfig.OMS_DB)
    public List<ApiGoodsForOrderVo> orderGetGoodsInfo(List<String> inventoryCodeList) {
        if (null == inventoryCodeList || CollectionUtils.isEmpty(inventoryCodeList)) {
            throw new IllegalArgumentException("inventoryCode is null");
        }
        List<ApiGoodsForOrderVo> ApiGoodsForOrderVoList = new ArrayList<ApiGoodsForOrderVo>();
        List<ApiGoodsForOrder> ApiGoodsForOrderList = apiGoodsForOrderMapper
                .orderGetGoodsInfo(inventoryCodeList);
        if (CollectionUtils.isNotEmpty(ApiGoodsForOrderList)) {
            for (ApiGoodsForOrder apiGoodsForOrder : ApiGoodsForOrderList) {
                ApiGoodsForOrderVo apiGoodsForOrderVo = new ApiGoodsForOrderVo();
                BeanUtils.copyProperties(apiGoodsForOrder, apiGoodsForOrderVo);
                // 规格
                apiGoodsForOrderVo.setSpecs(processSpecsStr(apiGoodsForOrder));
                /*if (apiGoodsForOrder.getInventoryType().equals(YesOrNoEnum.NO.getCode())) {
                    apiGoodsForOrderVo.setGoodsType(ApiGoodsForOrderVo.OperationTypeEnum.NEW);
                    apiGoodsForOrderVo.setInventoryTypeStr(ApiGoodsForOrderVo.OperationTypeEnum.NEW.getName());
                } else {
                    apiGoodsForOrderVo.setGoodsType(ApiGoodsForOrderVo.OperationTypeEnum.SECONDHAND);
                    apiGoodsForOrderVo.setInventoryTypeStr(ApiGoodsForOrderVo.OperationTypeEnum.SECONDHAND.getName());
                }*/

                if (apiGoodsForOrder.getInventoryType().equals(YesOrNoEnum.YES.getCode())) {
                    apiGoodsForOrderVo.setGoodsType(ApiGoodsForOrderVo.OperationTypeEnum.OLD);
                    apiGoodsForOrderVo.setInventoryTypeStr(ApiGoodsForOrderVo.OperationTypeEnum.OLD.getName());
                } else {
                    apiGoodsForOrderVo.setGoodsType(ApiGoodsForOrderVo.OperationTypeEnum.NEW);
                    apiGoodsForOrderVo.setInventoryTypeStr(ApiGoodsForOrderVo.OperationTypeEnum.NEW.getName());
                }

                // 销售模式转义为字符
                apiGoodsForOrderVo.setSalesModelStr(SalesModelEnum.getName(apiGoodsForOrder.getSalesModel()));
                // 将商品主图传给订单
                apiGoodsForOrderVo.setImageDetail(apiGoodsForOrder.getMainImage());
                ApiGoodsForOrderVoList.add(apiGoodsForOrderVo);
            }
        }
        return ApiGoodsForOrderVoList;
    }

    @Override
    public List<ApiGoodsForScmVo> scmGetGoodsInfo(List<String> inventoryCodeList) {
        List<ApiGoodsForScmVo> apiGoodsForScmVos = new ArrayList<>();
        try {
            List<ApiGoodsForOrderVo> apiGoodsForOrderVos = this.orderGetGoodsInfo(inventoryCodeList);
            if (CollectionUtils.isNotEmpty(apiGoodsForOrderVos)) {
                apiGoodsForScmVos = apiGoodsForOrderVos.stream().map(item -> {
                    ApiGoodsForScmVo apiGoodsForScmVo = new ApiGoodsForScmVo();
                    BeanUtils.copyProperties(item, apiGoodsForScmVo);
                    CloudTradeGoods tradeGoods = cloudTradeGoodsService.getCloudTradeGoodsByChannelGoodsCode(item.getInventoryCode());
                    if (ObjectUtils.isNotEmpty(tradeGoods) && ObjectUtils.isNotEmpty(tradeGoods.getAccountId())) {
                        apiGoodsForScmVo.setAccountId(tradeGoods.getAccountId());
                    } else {
                        apiGoodsForScmVo.setAccountId(0);
                    }
                    return apiGoodsForScmVo;
                }).collect(Collectors.toList());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("scmGetGoodsInfo--接口查询异常：{}", e);
            throw e;
        }
        return apiGoodsForScmVos;
    }

    /**
     * 处理规格字符数据
     *
     * @param apiGoodsForOrder
     * @return
     */
    private String processSpecsStr(ApiGoodsForOrder apiGoodsForOrder) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isNotBlank(apiGoodsForOrder.getColor()) && StringUtils.isNotBlank(apiGoodsForOrder.getMaterial())) {
            stringBuffer.append(apiGoodsForOrder.getColor());
            stringBuffer.append("-");
            stringBuffer.append(apiGoodsForOrder.getMaterial());
        } else if (StringUtils.isNotBlank(apiGoodsForOrder.getColor())) {
            stringBuffer.append(apiGoodsForOrder.getColor());
        } else if (StringUtils.isNotBlank(apiGoodsForOrder.getMaterial())) {
            stringBuffer.append(apiGoodsForOrder.getMaterial());
        }
        return stringBuffer.toString();
    }
}
